package com.yfyszk.kingbird.service.queue;

import com.yfyszk.kingbird.entity.SystemLog;
import com.yfyszk.kingbird.service.ISystemLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author wgc
 * @date 2019-09-03 16:07
 * 消费队列
 */
@Slf4j
@Component
public class ConsumerSystemLogQueue {
    @Autowired
    private ISystemLogService systemlogService;

    @PostConstruct
    public void startrtThread() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);//两个大小的固定线程池
        executorService.submit(new PollSystemLog(systemlogService));
        executorService.submit(new PollSystemLog(systemlogService));
    }

    class PollSystemLog implements Runnable {

        ISystemLogService systemLogService;

        public PollSystemLog(ISystemLogService systemLogService) {
            this.systemLogService = systemLogService;
        }

        @Override
        public void run() {
            while (true) {
                SystemLog systemLog = SystemLogQueue.getInstance().poll();
                log.info("日志信息:{}", systemLog);
                if (systemLog != null) {
                    systemlogService.save(systemLog);
                }

            }
        }
    }

}
